# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.common.action_chains import ActionChains
import unittest, time, re,sys
reload(sys)
sys.setdefaultencoding('utf-8')

class CheckIn(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(3)
        self.base_url = "http://www.189.cn/sc"
        self.second_url = "http://login.189.cn/login"
        self.verificationErrors = []
        self.accept_next_alert = True
    
    def test_CheckIn1625(self):
        driver = self.driver
        # driver.get(self.base_url)
        driver.get(self.second_url)
        time.sleep(2)
        driver.find_element_by_id("txtAccount").clear()
        driver.find_element_by_id("txtAccount").send_keys("18180351625")
        # driver.find_element_by_xpath("//*[@id='txtShowPwd']").clear()
        driver.find_element_by_xpath("//*[@id='txtShowPwd']").send_keys("463527")
        driver.find_element_by_id("loginbtn").click()
        # swait_text(driver,u"费用")
        time.sleep(2)
        target=driver.find_element_by_link_text(u"费用")
        ActionChains(driver).move_to_element(target).perform()
        time.sleep(1)
        driver.find_element_by_partial_link_text(u"余额查询").click()
        time.sleep(2)
        # 切换浏览器窗口
        nowhandle=driver.current_window_handle
        allhandles=driver.window_handles
        for handle in allhandles:
            if handle != nowhandle:
                driver.switch_to_window(handle)
        time.sleep(5)
        driver.find_element_by_id("77169890c56011e567faf0ef94b55375").click()
        time.sleep(5)
        driver.find_element_by_id("20000335").click()
        time.sleep(3)
        driver.switch_to_frame("bodyIframe")
        print "find iframe 'bodyIframe'"
        driver.find_element_by_id("current_qdljf").click()
        try: 
            driver.find_element_by_id("current_qdljf").click()
            time.sleep(3)
            self.assertEqual(u"您已成功领取5积分。", self.close_alert_and_get_its_text())
            print u"签到成功"              
        except NoSuchElementException:
            print u"pass，今天已签到"

        # if driver.find_element_by_id("current_qdljf"):
            # driver.find_element_by_id("current_qdljf").click()
            # time.sleep(3)
            # self.assertEqual(u"您已成功领取5积分。", self.close_alert_and_get_its_text())
            # print u"签到成功"
        # elif driver.find_element_by_id("current_jrylq"):
            # print u"今天已签到"
        # else:
            # print u"签到失败"

    # def test_CheckIn5382(self):
        # driver = self.driver
        # driver.get(self.second_url)
        # time.sleep(2)
        # driver.find_element_by_id("txtAccount").clear()
        # driver.find_element_by_id("txtAccount").send_keys("18123295382")
        # driver.find_element_by_xpath("//*[@id='txtShowPwd']").send_keys("463527")
        # driver.find_element_by_id("loginbtn").click()
        # time.sleep(2)
        # target=driver.find_element_by_link_text(u"费用")
        # ActionChains(driver).move_to_element(target).perform()
        # time.sleep(1)
        # driver.find_element_by_partial_link_text(u"余额查询").click()
        # time.sleep(2)
        # nowhandle=driver.current_window_handle
        # allhandles=driver.window_handles
        # for handle in allhandles:
            # if handle != nowhandle:
                # driver.switch_to_window(handle)
        # time.sleep(5)
        # driver.find_element_by_id("77169890c56011e567faf0ef94b55375").click()
        # time.sleep(3)
        # driver.find_element_by_id("20000335").click()
        # time.sleep(3)
        # driver.switch_to_frame("bodyIframe")
        # print "find iframe 'bodyIframe'"
        # try: 
            # driver.find_element_by_id("current_qdljf").click()
            # time.sleep(3)
            # self.assertEqual(u"您已成功领取5积分。", self.close_alert_and_get_its_text())
            # print u"签到成功"              
        # except NoSuchElementException:
            # print u"pass，今天已签到"

        # if driver.find_element_by_id("current_qdljf"):
            # driver.find_element_by_id("current_qdljf").click()
            # time.sleep(3)
            # self.assertEqual(u"您已成功领取5积分。", self.close_alert_and_get_its_text())
            # print u"签到成功"
        # elif driver.find_element_by_id("current_jrylq"):
            # print u"今天已签到"
        # else:
            # print u"签到失败"
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e: return False
        return True
    
    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException as e: return False
        return True
    
    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True
    
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
